import 'dart:async';

import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_love/net/dio_manager.dart';
import 'package:flutter_love/utils/progress_dialog_util.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';

class RegisterView extends StatefulWidget {
  const RegisterView({Key? key}) : super(key: key);

  @override
  _RegisterViewState createState() => _RegisterViewState();
}

class _RegisterViewState extends State<RegisterView> {
  late TextEditingController _controllerAccount;
  late TextEditingController _controllerPassWord;
  late TextEditingController _controllerConfirmPassWord;
  late FocusNode _focusNodeAccount;
  late FocusNode _focusNodePassWord;
  late FocusNode _focusNodeConfirmPassWord;
  final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();

  @override
  void initState() {
    super.initState();
    _controllerAccount = TextEditingController();
    _controllerPassWord = TextEditingController();
    _controllerConfirmPassWord = TextEditingController();
    _focusNodeAccount = FocusNode();
    _focusNodePassWord = FocusNode();
    _focusNodeConfirmPassWord = FocusNode();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('注册'),
        centerTitle: true,
        elevation: 10,
      ),
      body: Padding(
          padding: const EdgeInsets.all(10),
          child: SingleChildScrollView(
            child: Column(
              children: [
                Image.asset(
                  "assets/images/main.jpg",
                  width: double.infinity,
                  height: ScreenUtil.getScreenH(context) / 3,
                  fit: BoxFit.fill,
                ),
                const SizedBox(height: 10),
                TextField(
                  controller: _controllerAccount,
                  focusNode: _focusNodeAccount,
                  decoration: InputDecoration(
                      labelText: '账号',
                      hintText: '请输入账号',
                      prefixIcon: const Icon(Icons.person),
                      suffixIcon: IconButton(
                        onPressed: () {
                          _controllerAccount.clear();
                        },
                        icon: const Icon(Icons.close),
                      )),
                  keyboardType: TextInputType.text,
                  textInputAction: TextInputAction.next,
                ),
                const SizedBox(height: 10),
                TextField(
                  obscureText: true,
                  controller: _controllerPassWord,
                  focusNode: _focusNodePassWord,
                  decoration: InputDecoration(
                      labelText: '密码',
                      hintText: '请输入密码',
                      prefixIcon: const Icon(Icons.lock),
                      suffixIcon: IconButton(
                        onPressed: () {
                          _controllerPassWord.clear();
                        },
                        icon: const Icon(Icons.close),
                      )),
                  keyboardType: TextInputType.text,
                  textInputAction: TextInputAction.send,
                  onSubmitted: (value) {
                    LogUtil.e(value);
                  },
                ),
                const SizedBox(height: 10),
                TextField(
                  obscureText: true,
                  controller: _controllerConfirmPassWord,
                  focusNode: _focusNodeConfirmPassWord,
                  decoration: InputDecoration(
                      labelText: '确认密码',
                      hintText: '请输入确认密码',
                      prefixIcon: const Icon(Icons.lock),
                      suffixIcon: IconButton(
                        onPressed: () {
                          _controllerConfirmPassWord.clear();
                        },
                        icon: const Icon(Icons.close),
                      )),
                  keyboardType: TextInputType.text,
                  textInputAction: TextInputAction.send,
                  onSubmitted: (value) {
                    LogUtil.e(value);
                  },
                ),
                const SizedBox(height: 20),
                SizedBox(
                  width: ScreenUtil.getScreenH(context),
                  child: ElevatedButton(
                    onPressed: () {
                      _register();
                    },
                    style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.all(Colors.red),
                      foregroundColor: MaterialStateProperty.all(Colors.white),
                    ),
                    child: const Text('注册'),
                  ),
                ),
              ],
            ),
          )),
    );
  }

  ///注册
  void _register() {
    if (_controllerAccount.text.isEmpty) {
      Fluttertoast.showToast(msg: '请输入账号', toastLength: Toast.LENGTH_SHORT);
      return;
    }
    if (_controllerPassWord.text.isEmpty) {
      Fluttertoast.showToast(msg: '请输入密码', toastLength: Toast.LENGTH_SHORT);
      return;
    }
    if (_controllerConfirmPassWord.text.isEmpty) {
      Fluttertoast.showToast(msg: '请输入确认密码', toastLength: Toast.LENGTH_SHORT);
      return;
    }
    if (!(_controllerConfirmPassWord.text == _controllerPassWord.text)) {
      Fluttertoast.showToast(msg: '输入的密码不一致', toastLength: Toast.LENGTH_SHORT);
      return;
    }
    ProgressDialogUtil.showProgress(context);
    DioManager.getInstance()?.post('/user/register', {
      'username': _controllerAccount.text,
      'password': _controllerPassWord.text,
      'repassword': _controllerConfirmPassWord.text,
    }, (data) {
      ProgressDialogUtil.dismiss(context);
      _prefs.then((value) {
        value.setString('username', _controllerAccount.text);
        value.setString('password', _controllerConfirmPassWord.text);
      });
      Fluttertoast.showToast(msg: '注册成功', toastLength: Toast.LENGTH_SHORT);
      Navigator.of(context).pop();
    }, (error) {
      ProgressDialogUtil.dismiss(context);
    });
  }
}
